package com.bbx.taxi.client.Activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.InjectView;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.base.BaseRequest;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.model.passanger.UserBuild;
import com.bbx.api.sdk.model.passanger.WeightOrVolumnBuild;
import com.bbx.api.sdk.model.passanger.Return.Info;
import com.bbx.api.sdk.model.passanger.Return.User;
import com.bbx.api.sdk.model.passanger.Return.WeightOrVolumn;
import com.bbx.api.sdk.model.passanger.Return.WeightOrVolumn.WVDetails;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Recommen.Users;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.passeger.conn.GetUserNet;
import com.bbx.api.sdk.net.passeger.conn.VolumnNet;
import com.bbx.api.sdk.net.passeger.conn.WeightNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Menu.RecommenContentActivity.MyAdapter.ViewHolder;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Attribute.ChangeAttribute;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.Task.DetailsOfGoodsTask;
import com.bbx.taxi.client.Task.DetailsOfGoodsTask.OnFinishListener;
import com.bbx.taxi.client.widget.MaxByteLengthEditText;
import com.bbx.taxi.client.widget.Dailog.MyWVDetailsDailog;
import com.bbx.taxi.client.widget.Dailog.MyWVDetailsDailog.OnWVDetailsItemClickListener;

/**
 * 货品详情
 */
public class DetailsOfGoodsActivity extends BaseBBXActivity implements
		OnClickListener, OnFinishListener {
	@InjectView(R.id.layout_weight)
	RelativeLayout layout_weight;
	@InjectView(R.id.layout_volume)
	RelativeLayout layout_volume;
	@InjectView(R.id.et_message)
	MaxByteLengthEditText et_message;
	@InjectView(R.id.tv_weight)
	TextView tv_weight;
	@InjectView(R.id.tv_volume)
	TextView tv_volume;
	MyWVDetailsDailog mMyVolumeDailog;
	MyWVDetailsDailog mMyWeightDailog;
	private int volume;
	private int weight;
	private int intentType;
	private String split = "\n";
	private DetailsOfGoodsTask mDetailsOfGoodsTask;
	private WeightOrVolumn weightGson;
	private WeightOrVolumn volumnGson;

	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_detailsofgoods);
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initView() {
		// 货品详情
		setTitle(true, getString(R.string.details_of_goods), -1,
				getString(R.string.titile_right_finish));
		et_message.setMaxByteLength(60);
		//主要监听要放在实例的前面
		DetailsOfGoodsTask.setOnFinishListener(this);
		mDetailsOfGoodsTask = DetailsOfGoodsTask
				.getInstance(this, mApplication);

		layout_weight.setOnClickListener(this);
		layout_volume.setOnClickListener(this);
	}

	@Override
	protected void initData() {

	}

	public void getIntentView() {
		if (getIntent().getExtras() != null) {
			intentType = getIntent().getExtras().getInt(MainMsg.extra_message);
			int goods_weight = 0;// 物品重量
			int goods_volume = 0;// 物品体积
			String message_kj = null;
			switch (intentType) {
			case MainMsg.MESSAGE_MAIN_KJ:
				MainAttribute main = MainAttribute.getInstance();
				goods_weight = main.getGoodsWeight();
				goods_volume = main.getGoodsVolume();
				message_kj = main.getMessage_KJ();
				break;
			case MainMsg.MESSAGE_CHANGE_KJ:
				ChangeAttribute change = ChangeAttribute.getInstance();
				goods_weight = change.getGoodsWeight();
				goods_volume = change.getGoodsVolume();
				message_kj = change.getMessage_KJ();
				break;
			}

			if (message_kj != null && !message_kj.equals("")) {
				et_message.setText(message_kj);
				et_message.setSelection(et_message.length());
			}
			if (weightGson != null) {
				List<WVDetails> list = weightGson.getList();
				for (int i = 0; i < list.size(); i++) {
					if (goods_weight == list.get(i).getId()) {
						weight =list.get(i).getId();
						tv_weight.setText(list.get(i).getDesc());
					}
				}
			} else {
				if (goods_weight == Value.KJ_WEIGHT_0_5) {
					tv_weight.setText(getString(R.string.kg_0_5));
					weight = Value.KJ_WEIGHT_0_5;			
				} else if (goods_weight == Value.KJ_WEIGHT_6_10) {
					tv_weight.setText(getString(R.string.kg_6_10));
					weight = Value.KJ_WEIGHT_6_10;


				} else if (goods_weight == Value.KJ_WEIGHT_11_15) {
					tv_weight.setText(getString(R.string.kg_11_15));
					weight = Value.KJ_WEIGHT_11_15;
				}
				weightGson = new WeightOrVolumn();
				List<WVDetails> list = new ArrayList<WVDetails>();
				WVDetails mWVDetails = new WeightOrVolumn().new WVDetails();
				mWVDetails.setId(Value.KJ_WEIGHT_0_5);
				mWVDetails.setDesc(getString(R.string.kg_0_5));
				list.add(mWVDetails);
				 mWVDetails = new WeightOrVolumn().new WVDetails();
				mWVDetails.setId(Value.KJ_WEIGHT_6_10);
				mWVDetails.setDesc(getString(R.string.kg_6_10));
				list.add(mWVDetails);
				 mWVDetails = new WeightOrVolumn().new WVDetails();
				mWVDetails.setId(Value.KJ_WEIGHT_11_15);
				mWVDetails.setDesc(getString(R.string.kg_11_15));
				list.add(mWVDetails);
				weightGson.setList(list);
			}
			initPopViewWeight(weightGson);

			if (volumnGson != null) {
				List<WVDetails> lits = volumnGson.getList();
				for (int i = 0; i < lits.size(); i++) {
					if (goods_volume == lits.get(i).getId()) {
						volume =lits.get(i).getId();
						String content=lits.get(i).getDesc();
						if(content.contains(split)){
							tv_volume.setText(content.split(split)[0]);
						}
						else{
							tv_volume.setText(content);	
						}

					}
				}
			} else {
				if (goods_volume == Value.KJ_VOLUME_S) {
					tv_volume.setText(getString(R.string.goods_small));
					volume = Value.KJ_VOLUME_S;
				} else if (goods_volume == Value.KJ_VOLUME_M) {
					tv_volume.setText(getString(R.string.goods_middle));
					volume = Value.KJ_VOLUME_M;
				} else if (goods_volume == Value.KJ_VOLUME_B) {
					tv_volume.setText(getString(R.string.goods_big));
					volume = Value.KJ_VOLUME_B;
				}
				volumnGson = new WeightOrVolumn();
				List<WVDetails> list = new ArrayList<WVDetails>();
				WVDetails mWVDetails = new WeightOrVolumn().new WVDetails();
				mWVDetails.setId(Value.KJ_VOLUME_S);
				mWVDetails.setDesc(getString(R.string.goods_small)+"\n"+getString(R.string.goods_small_content));
				list.add(mWVDetails);
				 mWVDetails = new WeightOrVolumn().new WVDetails();
				mWVDetails.setId(Value.KJ_VOLUME_M);
				mWVDetails.setDesc(getString(R.string.goods_middle)+"\n"+getString(R.string.goods_middle_content));
				list.add(mWVDetails);
				 mWVDetails = new WeightOrVolumn().new WVDetails();
				mWVDetails.setId(Value.KJ_VOLUME_B);
				mWVDetails.setDesc(getString(R.string.goods_big)+"\n"+getString(R.string.goods_big_content));
				list.add(mWVDetails);
				volumnGson.setList(list);
			}
			initPopViewVolume(volumnGson);

		}
	}

	

	/**
	 * 重量
	 */
	public void initPopViewWeight(final WeightOrVolumn mWeightOrVolumn) {
		final List<WVDetails> list = mWeightOrVolumn.getList();
		mMyWeightDailog	=new MyWVDetailsDailog(this);
		mMyWeightDailog.setTitle(this.getResources().getString(R.string.weight));
		mMyWeightDailog.setAdapter(list);
		mMyWeightDailog.setOnWVDetailsItemClickListener(new OnWVDetailsItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				weight = list.get(position).getId();
				String content = list.get(position).getDesc();
				tv_weight.setText(content);
				mMyWeightDailog.dismiss();
			}
		});
	}

	/**
	 * 体积
	 */
	public void initPopViewVolume(final WeightOrVolumn mWeightOrVolumn) {
		final List<WVDetails> list = mWeightOrVolumn.getList();
		mMyVolumeDailog	=new MyWVDetailsDailog(this);
		mMyVolumeDailog.setTitle(this.getResources().getString(R.string.volume));
		mMyVolumeDailog.setAdapter(list);
		mMyVolumeDailog.setOnWVDetailsItemClickListener(new OnWVDetailsItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				volume = list.get(position).getId();
				String content = list.get(position).getDesc();
				if (content.contains(split)) {
					tv_volume.setText(content.split(split)[0]);
				} else {
					tv_volume.setText(content);
				}
				mMyVolumeDailog.dismiss();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_right:
			if (tv_weight.getText().toString().equals("")
					|| tv_weight.getText().toString()
							.equals(getString(R.string.select_weight))) {
				Toast.makeText(this, getString(R.string.select_weight),
						Toast.LENGTH_SHORT).show();
				return;
			}
			if (tv_volume.getText().toString().equals("")
					|| tv_volume.getText().toString()
							.equals(getString(R.string.select_volume))) {
				Toast.makeText(this, getString(R.string.select_volume),
						Toast.LENGTH_SHORT).show();
				return;
			}
			String message = et_message.getText().toString().toString();
			switch (intentType) {
			case MainMsg.MESSAGE_MAIN_KJ:
				MainAttribute main = MainAttribute.getInstance();			
				main.setGoodsWeight(weight);
				main.setGoodsVolume(volume);
				main.setMessage_KJ(message);
				break;
			case MainMsg.MESSAGE_CHANGE_KJ:
				ChangeAttribute change = ChangeAttribute.getInstance();
				change.setGoodsWeight(weight);
				change.setGoodsVolume(volume);
				change.setMessage_KJ(message);
				break;
			}

			Bundle bundle = new Bundle();
			Intent intent = new Intent();
			intent.putExtras(bundle);
			setResult(resultCode, intent);
			finish();
			break;
		case R.id.layout_weight:
			if (mMyWeightDailog != null && !mMyWeightDailog.isShowing()) {
				mMyWeightDailog.show();
			}
			break;
		case R.id.layout_volume:
			if (mMyVolumeDailog != null && !mMyVolumeDailog.isShowing()) {
				mMyVolumeDailog.show();
			}
			break;
		default:
			super.onClick(v);
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	@Override
	public void onFinish() {
		String weight_json = SharedPreUtil.getStringValue(this,
				SharedPreEncryptUtil.weight_json, "");
		String volume_json = SharedPreUtil.getStringValue(this,
				SharedPreEncryptUtil.volume_json, "");
		weightGson = (WeightOrVolumn) new JsonBuild().getData(
				WeightOrVolumn.class, weight_json);
		volumnGson = (WeightOrVolumn) new JsonBuild().getData(
				WeightOrVolumn.class, volume_json);
		getIntentView();
	}
}
